class Api::V1::PicturesController < Api::V1::BaseController
  before_action :authenticate_request!

  def index
    @pic = apply_scopes(Picture).all
    @pic = paginate(@pic)
    render json: @pic, meta: paginate_meta(@pic)
  end

  def show
    @pic = Picture.find(params[:id])
    render json: @pic, status: 200
  end

  def create
    begin
      @pic = Picture.new(create_params)
      authorize @pic
      @pic.save!
      render json: @pic, status: 201
    rescue ActiveRecord::StatementInvalid
      @pic.errors.add(:exec_error, 'Contact Administrator')
      error_render(@pic, 403)
    rescue => err
      error_render(err.record, 403)
    end
  end

  def update
    begin
      @pic = Picture.find(params[:id])
      @pic.update_attributes!(create_params)
      render json: @pic, status: 200
    rescue => err
      error_render(err.record, 403)
    end
  end

  def destroy
    begin
      @pic = Picture.find(params[:id])
      @pic.destroy!
      render status: 204
    rescue ActiveRecord::RecordNotFound
      render status: 404
    rescue
      render status: 403
    end
  end

  private

  def create_params
#    params.require(:data).permit(:type, :id, attributes: [:cert, :email, :phone_num, :password, :password_confirmation, :activity, :role_id, :company_id])
    params_parse(params)
  end

  def relationship_role
    params.require(:data).require(:relationships).require(:role).require(:data).permit(:id, :type)
  end

  def realtionship_company
    params.require(:data).require(:relationships).require(:company).require(:data).permit(:id, :type)
  end

end
